/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdministradorUI.java
 *
 * Created on 27/05/2009, 10:30:48 PM
 */

package CRM_UI;

import DAO.*;
import LogicaNegocio.Departamento;
import LogicaNegocio.Empleado;
import LogicaNegocio.JefeDeArea;
import java.util.Vector;
import javax.swing.JOptionPane;
/**
 *
 * @author L¡z
 */
public class AdministradorUI extends javax.swing.JFrame {

    String cedulaSeleccionada = "";
    LoginUI cambiarPerfil;
    /** Creates new form AdministradorUI */
    public AdministradorUI(LoginUI cambiarPerfil) {
        initComponents();
        consultarJefes();
        setLocationRelativeTo(null);
        this.cambiarPerfil=cambiarPerfil;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jefesPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jefesTable = new javax.swing.JTable();
        gestionarPanel = new javax.swing.JPanel();
        nombreLabel = new javax.swing.JLabel();
        cedulaLabel = new javax.swing.JLabel();
        departamentoLabel = new javax.swing.JLabel();
        cbDepartamento = new javax.swing.JComboBox();
        tfNombre = new javax.swing.JTextField();
        tfCedula = new javax.swing.JTextField();
        modificarButton = new javax.swing.JButton();
        nuevoButton = new javax.swing.JButton();
        botonPerfil = new javax.swing.JButton();

        jButton1.setText("crear");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Administrador");

        jefesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Jefes"));

        jefesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Cédula", "Nombre", "Departamento"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jefesTable.setAutoscrolls(false);
        jefesTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jefesTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jefesTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jefesTable);
        jefesTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        javax.swing.GroupLayout jefesPanelLayout = new javax.swing.GroupLayout(jefesPanel);
        jefesPanel.setLayout(jefesPanelLayout);
        jefesPanelLayout.setHorizontalGroup(
            jefesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jefesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 392, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );
        jefesPanelLayout.setVerticalGroup(
            jefesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jefesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        gestionarPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Gestionar Jefe"));

        nombreLabel.setText("Nombre:");

        cedulaLabel.setText("Cédula:");

        departamentoLabel.setText("Departamento:");

        cbDepartamento.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Servicio al Cliente", "Ventas", "Mercadeo" }));

        modificarButton.setText("Modificar");
        modificarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modificarButtonActionPerformed(evt);
            }
        });

        nuevoButton.setText("Nuevo");

        javax.swing.GroupLayout gestionarPanelLayout = new javax.swing.GroupLayout(gestionarPanel);
        gestionarPanel.setLayout(gestionarPanelLayout);
        gestionarPanelLayout.setHorizontalGroup(
            gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gestionarPanelLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nombreLabel)
                    .addComponent(cedulaLabel)
                    .addComponent(departamentoLabel))
                .addGap(41, 41, 41)
                .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(tfCedula, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                        .addComponent(tfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(93, Short.MAX_VALUE))
            .addGroup(gestionarPanelLayout.createSequentialGroup()
                .addGap(75, 75, 75)
                .addComponent(modificarButton)
                .addGap(70, 70, 70)
                .addComponent(nuevoButton)
                .addContainerGap(154, Short.MAX_VALUE))
        );
        gestionarPanelLayout.setVerticalGroup(
            gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gestionarPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombreLabel)
                    .addComponent(tfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cedulaLabel)
                    .addComponent(tfCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(departamentoLabel)
                    .addComponent(cbDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(modificarButton)
                    .addComponent(nuevoButton))
                .addContainerGap())
        );

        botonPerfil.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/cambiarPerfil.png"))); // NOI18N
        botonPerfil.setToolTipText("Permite al usuario cambiar de perfil");
        botonPerfil.setFocusPainted(false);
        botonPerfil.setRequestFocusEnabled(false);
        botonPerfil.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonPerfilActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(gestionarPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jefesPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(botonPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(botonPerfil)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jefesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(gestionarPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void modificarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modificarButtonActionPerformed
        DAOEmpleado dao = new DAOEmpleado();
        
        Empleado elEmpleado = new Empleado();
        String cedulaNueva = tfCedula.getText();
        elEmpleado.setCcEmpleado(cedulaNueva);
        elEmpleado.setNombreEmpleado(tfNombre.getText());        
        elEmpleado.setDepartamento(new Departamento((String)cbDepartamento.getSelectedItem()));

        int filaSeleccionada = jefesTable.getSelectedRow();
        String departamentoActual = (String)cbDepartamento.getSelectedItem();
        String departamentoUno = "", departamentoDos = "";
        for(int i = 0; i < 3; i++) {
            if(i != filaSeleccionada && departamentoUno.equals("")) {
                departamentoUno = (String)jefesTable.getValueAt(i, 2);
            }
            else if(i != filaSeleccionada && !(departamentoUno.equals(""))) {
                departamentoDos = (String)jefesTable.getValueAt(i, 2);
            }
        }
        
        if(departamentoActual.equals(departamentoUno) || departamentoActual.equals(departamentoDos)) {
            JOptionPane.showMessageDialog(this, "No se puede ingresar dos jefes por departamento.", "Error", JOptionPane.ERROR_MESSAGE);
        }
        else {
            if(cedulaNueva.equals(cedulaSeleccionada)) {
                JefeDeArea jefe = new JefeDeArea(elEmpleado.getCcEmpleado(), elEmpleado.getNombreEmpleado(), elEmpleado.getDepartamento(), cedulaSeleccionada);
                dao.modificarJefe(jefe);
            }
            else {
                dao.cambiarJefe(cedulaSeleccionada, elEmpleado);
                DAOLogin daoLogin = new DAOLogin();
                daoLogin.cambiarPerfilJefe(cedulaSeleccionada, elEmpleado);
            }
            consultarJefes();
        }
}//GEN-LAST:event_modificarButtonActionPerformed

    private void jefesTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jefesTableMouseClicked
        cedulaSeleccionada = (String)jefesTable.getValueAt(jefesTable.getSelectedRow(), 0);
        tfCedula.setText(cedulaSeleccionada);
        DAOEmpleado dao = new DAOEmpleado();
        Empleado elEmpleado = dao.consultarEmpleado(cedulaSeleccionada);
        tfNombre.setText(elEmpleado.getNombreEmpleado());
        cbDepartamento.setSelectedItem(elEmpleado.getDepartamento().getNombreDepartamento());
    }//GEN-LAST:event_jefesTableMouseClicked

    private void botonPerfilActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonPerfilActionPerformed
        // TODO add your handling code here:
        this.dispose();
        cambiarPerfil.limpiarCampos();
        cambiarPerfil.setVisible(true);
}//GEN-LAST:event_botonPerfilActionPerformed


    private void consultarJefes() {
        DAOEmpleado dao = new DAOEmpleado();
        Vector<JefeDeArea> losJefes = dao.consultarJefes();
        for(int i = 0; i < losJefes.size(); i++) {
            for(int j = 0; j < 3; j++) {
                switch(j) {
                    case 0:
                        jefesTable.setValueAt(losJefes.elementAt(i).getId_jefe(), i, j);
                        break;
                    case 1:
                        jefesTable.setValueAt(losJefes.elementAt(i).getNombreEmpleado(), i, j);
                        break;
                    case 2:
                        jefesTable.setValueAt(losJefes.elementAt(i).getDepartamento().getNombreDepartamento(), i, j);
                        break;
                }
            }
        }
    }

    /**
    * @param args the command line arguments
    */

  /*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AdministradorUI().setVisible(true);
            }
        });
    } */

     
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonPerfil;
    private javax.swing.JComboBox cbDepartamento;
    private javax.swing.JLabel cedulaLabel;
    private javax.swing.JLabel departamentoLabel;
    private javax.swing.JPanel gestionarPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jefesPanel;
    private javax.swing.JTable jefesTable;
    private javax.swing.JButton modificarButton;
    private javax.swing.JLabel nombreLabel;
    private javax.swing.JButton nuevoButton;
    private javax.swing.JTextField tfCedula;
    private javax.swing.JTextField tfNombre;
    // End of variables declaration//GEN-END:variables

}
